<?php

namespace app\admin\controller\shop;

use addons\wechat\library\Config as ConfigService;
use app\admin\model\Shop;
use app\admin\model\Shopuser;
use app\admin\model\User;
use app\admin\model\Useramount;
use app\admin\model\Usermsgweixin;
use app\common\controller\Backend;
use EasyWeChat\Factory;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Shopjob extends Backend
{
    
    /**
     * Shopjob模型对象
     * @var \app\admin\model\Shopjob
     */
    protected $model = null;
    protected $wxapp = null;


    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Shopjob;
        $this->wxapp = Factory::officialAccount(ConfigService::load());


    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model->with(['worktype','area','shop'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $k => $v) {
                if($v['status'] == -1){
                    $list[$k]['status'] = "未审核";
                }else if($v['status'] == 1){
                    $list[$k]['status'] = "审核通过";
                }else if($v['status'] == 0){
                    $list[$k]['status'] = "审核拒绝";
                }
            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);

        if ($this->request->isPost()) {

            $params = $this->request->post("row/a");

            $status = $params['status'];
            $statusTxt = "";
            if($status == 1){
                $statusTxt= "审核通过";
            }else{
                $statusTxt= "未能通过审核";

            }
            $shopId = $row['companyid'];
            $shopModel = new Shop();
            $shop  = $shopModel->where('id',$shopId)->find();
            $uid = $shop['uid'];
            $usermsgModel = new Usermsgweixin();
            $refWx  = $usermsgModel->where('user_id',$uid)->find();


            error_log("====wx======".json_encode($refWx));

            if($refWx){
                $t = $this->wxapp->template_message->send([
                    'touser' => $refWx['openid'],
                    'template_id' => 'LCt1NrqqSbCP8MJx4dBFw1NtQhE6D7_m0X_JIVhZof4',
                    'data' => [
                        'first' => "尊敬的 ".$shop['merchantName']."，您好！ 您所发布的职位".$statusTxt,
                        'keyword1' => $row['title'],
                        'keyword2' => $statusTxt,
                        'keyword3' => "推荐入职奖励",
                        'keyword4' =>  "",
                        'remark' => '更多信息，请查看百狮人力商家小程序',
                    ],
                ]);

            }

            $shopAdminModel = new \app\admin\model\Shopadmin();
            $shopAdmin = $shopAdminModel->where('shopid',$shopId)->find();
            if($shopAdmin){
                $adminWx  = $usermsgModel->where('user_id',$shopAdmin['uid'])->find();
                if($adminWx){
                    $t = $this->wxapp->template_message->send([
                        'touser' => $adminWx['openid'],
                        'template_id' => 'LCt1NrqqSbCP8MJx4dBFw1NtQhE6D7_m0X_JIVhZof4',
                        'data' => [
                            'first' => "尊敬的 ".$shop['merchantName']."，您好！ 您所发布的职位".$statusTxt,
                            'keyword1' => $row['title'],
                            'keyword2' => $statusTxt,
                            'keyword3' => "推荐入职奖励",
                            'keyword4' =>  "",
                            'remark' => '更多信息，请查看百狮人力商家小程序',
                        ],
                    ]);
                }

            }

        }
        if (!$row) {
            $this->error(__('No Results were found'));
        }



        return parent::edit($ids);
    }
}
